package com.kgc.itrip.service.impl;

import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.common.Page;
import com.kgc.itrip.dao.SolrDao;
import com.kgc.itrip.entity.ItripHotelVO;
import com.kgc.itrip.service.HotelSearchService;
import com.kgc.itrip.vo.hotel.SearchHotCityVO;
import com.kgc.itrip.vo.hotel.SearchHotelVO;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HotelSearchServiceImpl implements HotelSearchService {
    @Resource
    private SolrDao<ItripHotelVO> solrDao;
    @Override
    public Page<ItripHotelVO> searchHotelByCondition(SearchHotelVO hotelVO) throws Exception {
        SolrQuery solrQuery = new SolrQuery("*:*");
        if(EmptyUtils.isNotEmpty(hotelVO)){
            //设置查询的目的地
            StringBuffer sb = new StringBuffer("destination:"+hotelVO.getDestination() );
            //设置关键字查询
            if(EmptyUtils.isNotEmpty(hotelVO.getKeywords())&&!hotelVO.getKeywords().equals("null")){
                String keywords = hotelVO.getKeywords();
                keywords = keywords.replaceAll(" ","|");
                sb.append(" AND keyword:"+keywords);
            }
            solrQuery.setQuery(sb.toString());
            //设置根据酒店等级查询
            if(EmptyUtils.isNotEmpty(hotelVO.getHotelLevel())){
                solrQuery.addFilterQuery(" hotelLevel:"+hotelVO.getHotelLevel());
            }
            //设置商圈的id来查询
            String tradeAreaIds = hotelVO.getTradeAreaIds();
            if(EmptyUtils.isNotEmpty(tradeAreaIds)){
                String[] areaIds = tradeAreaIds.split(",");
                for(int i=0; i<areaIds.length; i++){
                    if(i==0){
                        solrQuery.addFilterQuery(" tradingAreaIds:*,"+areaIds[i]+",*");
                    }else {
                        solrQuery.addFilterQuery(" OR tradingAreaIds:*,"+areaIds[i]+",*");
                    }

                }
            }
            //根据酒店的特色来查询
            String featureIds = hotelVO.getFeatureIds();
            if(EmptyUtils.isNotEmpty(featureIds)){
                String[] featureIdArr = featureIds.split(",");
                for(int i=0; i<featureIdArr.length;i++){
                    if(i==0){
                        solrQuery.addFilterQuery(" featureIds:*,"+featureIdArr[i]+",*");
                    }else {
                        solrQuery.addFilterQuery(" OR featureIds:*,"+featureIdArr[i]+",*");
                    }

                }
            }
            //根据价格区间查询
            if(EmptyUtils.isNotEmpty(hotelVO.getMaxPrice())){
                solrQuery.addFilterQuery(" minPrice:[* TO "+hotelVO.getMaxPrice()+"]");
            }
            if(EmptyUtils.isNotEmpty(hotelVO.getMinPrice())){
                solrQuery.addFilterQuery(" minPrice:["+hotelVO.getMinPrice()+" TO *]");
            }
            //设置升序查询的字段
            if(EmptyUtils.isNotEmpty(hotelVO.getAscSort())){
                solrQuery.setSort(hotelVO.getAscSort(),SolrQuery.ORDER.desc);
            }
            //设置降序查询的字段
            if(EmptyUtils.isNotEmpty(hotelVO.getDescSort())){
                solrQuery.setSort(hotelVO.getDescSort(), SolrQuery.ORDER.desc);
            }
        }
        Page<ItripHotelVO> Page = solrDao.searchByCondition(Constants.Solr_Core_Name.HOTEL_CORE, solrQuery, hotelVO.getPageNo(), hotelVO.getPageSize(), ItripHotelVO.class);
        return Page;
    }

    @Override
    public List<ItripHotelVO> searchItripHotelListByHotCity(SearchHotCityVO hotelVO) throws Exception {
        SolrQuery solrQuery =new SolrQuery("*:*");
        solrQuery.addFilterQuery("cityId:"+hotelVO.getCityId());
        List<ItripHotelVO> itripHotelVOS = solrDao.listQuery(Constants.Solr_Core_Name.HOTEL_CORE,solrQuery, hotelVO.getCount(), ItripHotelVO.class);
        return itripHotelVOS;
    }
}
